var pageii = null;
var grid = null;
var chek = 'root';
$(function() {	
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ 
            { colkey : "user_id", name : "用户id", hide : true, }, 
            { colkey : "user_name", name : "姓名",isSort:true,  align:'left'},
            { colkey : "user_account", name : "账号", isSort:true,  align:'left'}, 
            { colkey : "job_id", name : "工号", isSort:true,}, 
            { colkey : "email", name : "邮箱", isSort:true,  align:'left'},
            { colkey : "enterprise_qq", name : "企业QQ", align:'center'},
            { colkey : "state", name : "状态", width : '90px', isSort:true,
            	renderData:function( rowindex ,data, rowdata, colkey){
					if( rowdata["state"]=='1'){
						return "离职";
					}else if(rowdata["state"]=='0'){
						return "在职";
					}
				}
            }, 
            ],
		jsonUrl : rootPath +'/system/userInfo/findUserByDeptId.shtml?deptId='+chek,
		checkbox : true,
		serNumber : true,
		singleCheck:true,
		checkValue : 'user_id' 
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		if($("#leaderId").val()==''){
			searchParams["sysUserFormMap.leader_id"] = "";
		}
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addAccount").click("click", function() {
		addAccount();
	});
	$("#editAccount").click("click", function() {
		editAccount();
	});
	$("#delAccount").click("click", function() {
		delAccount();
	});
	$("#permissions").click("click", function() {
		permissions();
	});
	$("#role").click("click", function() {
		role();
	});

	$("#reset").click("click", function() {
		reset();
	});
	
	
});


function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		 tips("请选择一个用户",1);
		return;
	}
	showModal("user_info","编辑", rootPath + "/system/userInfo/editUI.shtml?userId="+cbox,680,450);
}
function addAccount() {
	var deptId =$("#user_dept_id").val();
	var deptName =$("#dept_name").val();
	if (deptId == " " || deptId == null) {
		  tips("请选择部门",1);
		return;
	}
	showModal("user_info","新增", rootPath + "/system/userInfo/addUI.shtml?deptId="+deptId+"&deptName="+deptName,680,450);
}
function delAccount() {
		var cbox = grid.getSelectedCheckbox();
		if (cbox == "") {
			tips("请选择需要禁用的项",1);
			return;
		}
		confirm('你确定要禁用吗？', function() {
			var url = rootPath + '/system/userInfo/deleteEntity.shtml';
			var s = CommnUtil.ajax(url, {
				ids : cbox.join(",")
			}, "json");
			if(s=="success"){
				tips("删除成功",1);
				loadData();
			}else{
				tips("删除失败",1);
			}
		});

	};
	
	
	function reset(){
		var cbox = grid.getSelectedCheckbox();
		if (cbox == "" || cbox >1) {
			tips("请选择需要重置的项",1);
			return;
		}
		
		confirm('你确定要重置该用户密码吗？', function() {
			var url = rootPath + '/system/userInfo/resetPwd.shtml';
			var s = CommnUtil.ajax(url, {
				ids : cbox.join(",")
			}, "json");
			if(s=="success"){
				tips("重置成功",1);
				loadData();
			}else{
				tips("重置失败",1);
			}
		});

	}
	
function loadData(){
	var searchParams = $("#searchForm").serializeJson();// 初始化传参数
	grid.setOptions({
		data : searchParams
	});
}

function role() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		 tips('请选中一个用户');
		return;
	}
	showModal("user_role","关联角色", rootPath + "/system/userInfo/roleUI.shtml?userId="+cbox,780,350);
}

function  onLoad(node){
	$("#user_dept_id").val(node.id);
	$("#dept_name").val(node.name);
   grid.setOptions({
	   jsonUrl:rootPath + '/system/userInfo/findUserByDeptId.shtml?deptId='+node.id
	});
	  
}

function clickUserList(list) {
	$("#leaderHiddenId").val(list.split(",")[0]);
	$("#leaderId").val(list.split(",")[1]);
}

